<template>
  <div style="height: 83.3vh; display: flex;flex-direction: column; justify-content:space-between">
    <div>
      <van-search
          shape="round"
          v-model="keyword"
          placeholder="找搭子"
          input-align="center"
          @search="onSearch"
      />
      <van-divider v-if="activeIds.length !== 0" style="position: relative;left:-40px" content-position="left">已选标签</van-divider>
      <div style="display: flex;flex-wrap: wrap;">
        <van-tag style="margin: 0 0 10px 10px" v-for="tag in activeIds" closeable size="large" type="primary" @close="onTagClose(tag)">
          {{ tag }}
        </van-tag>
      </div>
      <van-divider style="position: relative;left:-40px" content-position="left">选择标签</van-divider>
    </div>
    <van-tree-select
        style="flex: 1; overflow: auto"
        v-model:active-id="activeIds"
        v-model:main-active-index="activeIndex"
        :items="tagList"
    />
    <van-button style="width: 90%;left:5%;margin-top: 20px" type="primary" round block @click="search">搜索
    </van-button>
  </div>
</template>
<script setup lang="ts">
import {ref} from 'vue';
import {useRouter} from "vue-router";
import {showFailToast} from "vant";

const keyword = ref('');
const onSearch = () => {
  tagList.value = originalTagList.map(parentTag => {
    const tempChildren = [...parentTag.children];
    const tempParentTag = {...parentTag};
    tempParentTag.children = tempChildren.filter(childTag => childTag.text.includes(keyword.value));
    return tempParentTag;
  })
};
const activeIds = ref([]);
const activeIndex = ref(0);
const originalTagList = [
  {
    text: '语言',
    children: [
      {text: 'java', id: 'java'},
      {text: 'phyton', id: 'phyton'},
      {text: 'c++', id: 'c++'},
      {text: 'c#', id: 'c#'},
      {text: 'c', id: 'c'},
      {text: 'go', id: 'go'},
    ],
  },
  {
    text: '性别',
    children: [
      {text: '男', id: '男'},
      {text: '女', id: '女'},
      {text: '保密', id: '保密'},
    ],
  },
  {
    text: '年纪',
    children: [
      {text: '大一', id: '大一'},
      {text: '大二', id: '大二'},
      {text: '大三', id: '大三'},
      {text: '大四', id: '大四'},
      {text: '研一', id: '研一'},
      {text: '研二', id: '研二'},
      {text: '研三', id: '研三'},
    ],
  },
  {
    text: '工作状态',
    children: [
      {text: '待业', id: '待业'},
      {text: '在职', id: '在职'},
      {text: '在校', id: '在校'},
    ],
  },
  {
    text: '职位',
    children: [
      {text: '前端', id: '前端'},
      {text: '后端', id: '后端'},
      {text: '全栈', id: '全栈'},
      {text: '大数据', id: '大数据'},
      {text: '算法', id: '算法'},
    ],
  }
];
const tagList = ref(originalTagList);
const onTagClose = (tag) => {
  activeIds.value = activeIds.value.filter((t) => t !== tag);
}
const router = useRouter();
const search = () => {
  if (activeIds.value.length === 0) {
    showFailToast('请选择标签');
    return;
  }
  router.push({
    path: '/result',
    query: {
      tags: activeIds.value
    }
  });
}
</script>
